package me.muci.A_base.processingFlow;

import org.junit.Test;

import java.io.*;

/**
 * ClassName: BufferedTest <br/>
 * Description: 处理流之一 缓冲流的 使用<br/>
 * date: 2021/8/12 11:40<br/>
 *
 * 提供流的读取、写入速度，内部提供了一个缓冲区
 *
 * @author muci<br />
 * @description
 * @since JDK 1.8
 */
public class BufferedStreamTest {

    public void copy(String srcPath,String targetPath){
        File file = new File(srcPath);
        File file1 = new File(targetPath);
        //先创建内层节点流
        FileInputStream fis = null;
        FileOutputStream fos = null;

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            fos = new FileOutputStream(file1);
            //再创建处理流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //先关闭外层流(处理流）再关闭文件流,,外层流关闭时，会自动关闭其中的内层流
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void test(){
        long l = System.currentTimeMillis();
        System.out.println("开始复制，开始时间: " + l);
        copy("src\\main\\resources\\video.mp4","src\\main\\resources\\video_copy.mp4");
        long l1 = System.currentTimeMillis();
        System.out.println("复制结束，结束时间:+ " + l1);
        System.out.println(l1 - l);
        //2734
    }

}
